﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace MockingBird.Common.Infrastructure.Helpers
{
    public static class DirectoryHelper
    {
        public static void CopyFolders(string[] sourceFolderPaths, string destinationParentFolder)
        {
            foreach (string sourceFolderPath in sourceFolderPaths)
            {
                DirectoryInfo source = new DirectoryInfo(sourceFolderPath);
                string destinationFolderPath = Path.Combine(destinationParentFolder, source.Name);
                DirectoryInfo target = new DirectoryInfo(destinationFolderPath);
                CopyAll(source, target);

            }
        }
        public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
        {
            // Check if the target directory exists, if not, create it.
            if (Directory.Exists(target.FullName) == false)
            {
                Directory.CreateDirectory(target.FullName);

            }
            MakeFolderWritable(target.FullName);

            // Copy each file into it’s new directory.
            foreach (FileInfo fi in source.GetFiles())
            {
                string destinationFile = Path.Combine(target.ToString(), fi.Name);
                FileInfo targetInfo = new FileInfo(destinationFile);
                //remove the readonly property.
                fi.CopyTo(destinationFile, true);
                File.SetAttributes(destinationFile, FileAttributes.Normal);

            }

            // Copy each subdirectory using recursion.
            foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
            {
                DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir);
            }
        }

        public static void RemoveAll(string parentFolder, string searchPattern)
        {
            string[] foldersToRemove = Directory.GetDirectories(parentFolder, searchPattern);
            foreach (string folderName in foldersToRemove)
            {
                DirectoryInfo folder = new DirectoryInfo(folderName);
                foreach (FileInfo fi in folder.GetFiles())
                {
                    File.SetAttributes(fi.FullName, FileAttributes.Normal);
                }
                folder.Delete(true);
            }
        }

        private static void MakeFolderWritable(string Folder)
        {
            if (IsFolderReadOnly(Folder))
            {
                System.IO.DirectoryInfo oDir = new System.IO.DirectoryInfo(Folder);
                oDir.Attributes = oDir.Attributes & ~System.IO.FileAttributes.ReadOnly;
            }
        }


        private static bool IsFolderReadOnly(string Folder)
        {
            System.IO.DirectoryInfo oDir = new System.IO.DirectoryInfo(Folder);
            return ((oDir.Attributes & System.IO.FileAttributes.ReadOnly) > 0);
        }


    }
}
